package com.fingard.dsp.bank.directbank.bjums01;

import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IQRPay;
import com.fingard.dsp.bank.reqrespobj.qrpay.ReqGenQRInfo;
import com.fingard.dsp.bank.reqrespobj.qrpay.RetAtsGenQRInfo;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqUnifiedOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsUnifiedOrder;
import com.fingard.text.StringHelper;

import java.util.HashMap;
import java.util.Map;

public class BJUMS01QRPay extends BJUMS01UnifiedOrder implements IQRPay {
    @Override
    public RetAtsGenQRInfo genQRInfo(ReqGenQRInfo p_req) {
        return null;
    }

    @Override
    public RetAtsUnifiedOrder submitQROrder(ReqUnifiedOrder p_req) {
        RetAtsUnifiedOrder retAts = new RetAtsUnifiedOrder();
        retAts.rdSeq = p_req.rdSeq;
        try{
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_req.ownAct, "");
            if (actItem != null) {
                Map<String, Object> param = getParam(actItem);
                param.put("tranno",  BJUMS01TransType.QR001001);
                param.put("reqOrderNo",  p_req.rdSeq);
                param.put("merchId",  actItem.merchantId);
                String source ="";
                if ("NATIVE01".equalsIgnoreCase(p_req.reqHeader.commandCode)) {
                    //微信
                    source = "0";
                }else if("NATIVE02".equalsIgnoreCase(p_req.reqHeader.commandCode)){
                    //支付宝
                    source = "1";
                }else{
                    retAts.respCode = "-1";
                    retAts.respInfo = "未知的线路指令代码";
                    return retAts;
                }
                param.put("source",  source);
                param.put("amount",  StringHelper.amountMultiply100(p_req.amount));
                param.put("currency",   "CNY");
                String notify_url = actItem.notifyURL;
                if (StringHelper.hasAnyChar(notify_url)) {
                    param.put("notifyUrl", notify_url);//后台通知地
                }
                String clientIp = actItem.createIP;
                if (StringHelper.hasAnyChar(notify_url)) {
                    param.put("clientIp", clientIp);//后台通知地
                }
                param.put("subject",  StringHelper.hasAnyChar(p_req.productId)?p_req.productId:"人寿商编 C扫B测试");
                param.put("description", p_req.attach);
                param.put("timeExpire", p_req.timeExpire);
                boolean limit = getBankConfig().getValueAsBool("limitPay");
                WriteBankUrlLogLn("能否用信用卡支付："+limit);
                param.put("limitCreditCard", limit);
                param.put("body", p_req.description);
                String sign = addSign(null, param, actItem.saltKey);
                WriteBankLogLn("签名结果:"+sign);
                param.put("signature", sign);
                JSONObject jsonObj=new JSONObject(param);
                String[] tmpStrRet = sendToBank(jsonObj.toJSONString(),bankFront.serverURL);
                if(tmpStrRet[0].length() == 0){
                    JSONObject resultObj = JSONObject.parseObject(tmpStrRet[1]);
                    String  pay_result = resultObj.getString("respCode");
                    String resp_msg = resultObj.getString("respMsg");
                    if("000000".equals(pay_result)){
                        String qrcode = resultObj.getString("qrcode");
                        retAts.respCode = "0";
                        retAts.respInfo = "success";
                        retAts.qrInfo = qrcode;
                    }else {
                        retAts.respCode = "-1";
                        retAts.respInfo = resp_msg;
                    }

                }else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpStrRet[1];
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息!";
                retAts.qrInfo = "";
            }
        }catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        return retAts;
    }

    @Override
    public RetAtsQueryOrder queryQRPay(ReqQueryOrder p_req) {
        return queryUnifiedOrder(p_req);
    }
}
